function [] = trackingSystem()

    theta = 0; % actual angle of the radar antenna ...
    theta_R = []; % angle between target and the radar antenna ...
    K = linspace(0, 5, 1000); %[0.00001 ,0.025]%,0.5, 0.1, 0.01, 0.001, 0.0001]; % constant value of the torque controller for the antenna ...
    ki = 0;

    percError = [0.10, 0.05, 0.01, 0.001];

    

    %% constant parameters for the antenna:
    IM = 0.004; % inertia moment of the antenna in [kg m^2] ...
    b = 0.02; % viscosity coefficient [kg m^2 s^-1] ...
    
    %% nested functions:
    % model of the antenna:
    function x_prim = antennaModel(ta, ya)%, ki)
        x1 = ya(1);
        x2 = ya(2);
        theta_r = bearTarget(ta);
        
        % coefficient matrix A ...
        coeffMat_A = [0, 1, 0; -ki/IM, -b/IM, (ki*theta_r)/IM];
        % vector with unknown variables ...
        vec_x = [x1; x2; 1];
        % create the model of the system by applying the formula
        % dx/dt = A*x + b:
        %(note: here is b embedded in matrix A and vec_x.)
        x_prim = coeffMat_A * vec_x;
    end

    % method to bear the target (calculate the position = angle):
    function theta_r = bearTarget(t)
        theta_r = 0.01*t;
    end

    %% equation for tracking the target:
    %    IM*Theta'' = -b*Theta' + u(t)
    %    u(t) = K*e(t), e(t) = difference between "theta_R" and "theta"

    y0 = [0; 0];
    t_i = 0;        % initial value
    t_f = 300;      % final value
    
    fhandle = @antennaModel;

    clf;
    
    
    for percentIndex=1:length(percError)

        
        for i=2:length(K)       % 2 for skip k=0
            ki = K(i);
            
            [t, y] = ode45(fhandle, [t_i, t_f], y0); % RK-method supported by matlab ...
            %[t, y] = ode45(fhandle, tspan, y0); % RK-method supported by matlab ...
            % calculate the angle values between target and the radar antenna ...
            theta_R = bearTarget(t);

            relE = relError(y(:,1), theta_R);
            %plot(t,relE);           
            val=valueAtSpecificTime(t,relE,5.0);
            if(val< percError(percentIndex))
                fprintf('k : %f  error:%f percent:%f \n',ki,val,percError(percentIndex));
                break;
            end
            
%             hold on;
%             val=valueAtSpecificTime(t,relE,1.0);
%             if(val < 10 ) 
%                 fprintf(' k=  %f  para  porcentaje %f',ki,1);
%                 break;
%             end
            
       end
    end
    
    hold off;
    

    
    
    

    
    
    %% error methods:
    % calculates the signal error e(t):
    function e = signalError(theta, theta_R)
        e = abs(theta_R - theta);
       % e = theta_R - theta;
    end

    % relative error of the signals:
    function re = relError(theta, theta_R)
        re = signalError(theta, theta_R)./theta_R;
    end

    %% control methods to control the motor torques of the antenna:
    % simple controller to control the motor torques by using a (deterministic)
    % constant proportional value "K".
    function u =propController(theta, theta_R, K)
        u = K*signalError(theta, theta_R);
    end

    % integral controller: u(t) = k_i*int(e(tau), tau, 0, t)
    %
    % (--> will getting suck in the simulation ;-) )
    function u = intController(k_i, t)
        u = k_i*int(e(tau), 0, t);
    end

    % derivative controller: u(t) = k_d*(d e(t)/d t)
    %
    % (--> will return definitely the better solutions ...)
    function u = derivController(k_d, t)
        u = k_d*diff(e(t), t);
    end

    function u = valueAtSpecificTime(t,x,time)
        i=1;
        while(t(i)<time)
            i=i+1;
        end
        u=x(i);
    end


end

    
    
 